﻿using System;
using System.Collections.Generic;
using Assist;
using Autodesk.Revit.Attributes;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using YArchitech.Revit;
using YArchitech.Revit.RevitExtension;

namespace YJKArch.DimensionModule
{
	[Regeneration(RegenerationOption.Manual)]
	[Transaction(TransactionMode.Manual)]
	public class CmdArcDim : YJKCommandManual
	{
		protected override Result YJKExcute(ExternalCommandData commandData, ref string message, ElementSet elements)
		{
			UIApplication application = commandData.Application;
			UIDocument activeUIDocument = application.ActiveUIDocument;
			Autodesk.Revit.DB.Document document = activeUIDocument.Document;
			Level genLevel = document.ActiveView.GenLevel;
			XYZ startPoint = null;
			XYZ endPoint = null;
			Result result;
			for (;;)
			{
				Transaction transaction = new Transaction(document, "create");
				transaction.Start();
				try
				{
					startPoint = activeUIDocument.Selection.PickPoint("请选择一个点");
					endPoint = activeUIDocument.Selection.PickPoint("请选择第二个点");
				}
				catch (Exception)
				{
					transaction.RollBack();
					result = Autodesk.Revit.UI.Result.Succeeded;
					break;
				}
				Line line = YJKLineEx.YJKGetBound(startPoint, endPoint);
				IntersectionResultArray intersectionResultArray = null;
				List<Element> list = new List<Element>();
				try
				{
					IList<Element> collection = ElementFiltering.FindSpecialElements(document, typeof(Wall), BuiltInCategory.OST_Walls);
					list.AddRange(collection);
				}
				catch (Exception)
				{
				}
				try
				{
					IList<Element> collection2 = ElementFiltering.FindSpecialElements(document, typeof(CurveElement), BuiltInCategory.OST_Lines);
					list.AddRange(collection2);
				}
				catch (Exception)
				{
				}
				List<XYZ> list2 = new List<XYZ>();
				new ReferenceArray();
				foreach (Element element in list)
				{
					if (element is Wall)
					{
						Curve curve = ((element as Wall).Location as LocationCurve).Curve;
						if (curve.IsCyclic)
						{
							line.Intersect(curve, out intersectionResultArray);
							if (intersectionResultArray != null)
							{
								XYZ xyzpoint = intersectionResultArray.get_Item(0).XYZPoint;
								list2.Add(xyzpoint);
							}
						}
					}
					else if (element is ModelArc)
					{
						Curve curve2 = ((element as ModelArc).Location as LocationCurve).Curve;
						if (curve2.IsCyclic)
						{
							line.Intersect(curve2, out intersectionResultArray);
							if (intersectionResultArray != null)
							{
								XYZ xyzpoint2 = intersectionResultArray.get_Item(0).XYZPoint;
								list2.Add(xyzpoint2);
							}
						}
					}
				}
				if (list2.Count == 0)
				{
					TaskDialog.Show("错误", "没有相交的元素");
					transaction.RollBack();
				}
				else
				{
					for (int i = 0; i < list2.Count - 1; i++)
					{
						this.CreateLinearDimension(document, application, list2[i], list2[i + 1]);
					}
					transaction.Commit();
					list2.Clear();
				}
			}
			return result;
		}

		public Dimension CreateLinearDimension(Autodesk.Revit.DB.Document doc, UIApplication app, XYZ point1, XYZ point2)
		{
			Line line = YJKLineEx.YJKGetBound(point1, point2);
			Plane plane = RevitVersionFuncs.CreatePlanByNormalAndOrigin(point1.CrossProduct(point2), point2);
			SketchPlane sketchPlane = RevitVersionFuncs.NewSketchPlane(doc, plane);
			ModelCurve modelCurve = doc.Create.NewModelCurve(line, sketchPlane);
			ReferenceArray referenceArray = new ReferenceArray();
			referenceArray.Append(modelCurve.GeometryCurve.GetYJKEndPointReference(0));
			referenceArray.Append(modelCurve.GeometryCurve.GetYJKEndPointReference(1));
			XYZ startPoint = new XYZ(point1.X, point1.Y + 5.0, point1.Z);
			XYZ endPoint = new XYZ(point2.X, point2.Y + 5.0, point2.Z);
			line = YJKLineEx.YJKGetBound(startPoint, endPoint);
			return doc.Create.NewDimension(doc.ActiveView, line, referenceArray);
		}
	}
}
